package top.txl.controller;

import io.jsonwebtoken.Claims;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import top.txl.Dto.DishDto;

import top.txl.Dto.SetmealDto;
import top.txl.entity.Dish;
import top.txl.entity.PageBean;
import top.txl.entity.R;
import top.txl.exception.CustomerException;
import top.txl.service.DishFlavorService;
import top.txl.service.DishService;
import top.txl.utils.JwtUtils;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;

@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;


    /**
     * 添加dish数据
     *
     * @param dishDto
     * @return
     */

    @PostMapping("/add")
    public R<String> add(HttpServletRequest request, @RequestBody DishDto dishDto) {

        if (dishDto.getDescription().equals(null)) {
            dishDto.setDescription("");
        }

        String jwt = request.getHeader("Authorization");
        Claims claims = JwtUtils.parseJwt(jwt);
        Integer id = (Integer) claims.get("id");
        dishDto.setCreateTime(LocalDateTime.now());
        dishDto.setUpdateTime(LocalDateTime.now());
        dishDto.setCreateUser(id);
        dishDto.setUpdateUser(id);

        dishDto.setStatus(1);
        dishDto.setSort(0);
        dishDto.setIsDeleted(0);
        int i = (int) ((Math.random() * 1000000000L));
        log.info("code,{}", i);
        dishDto.setCode(i + "");

        dishService.add(dishDto);
        return R.success("新增成功");
    }

    @PutMapping("/edit")
    public R<String> update(HttpServletRequest request, @RequestBody DishDto dishDto){
        if (dishDto.getDescription().equals(null)) {
            dishDto.setDescription("");
        }

        String jwt = request.getHeader("Authorization");
        Claims claims = JwtUtils.parseJwt(jwt);
        Integer id = (Integer) claims.get("id");
        dishDto.setUpdateTime(LocalDateTime.now());
        dishDto.setUpdateUser(id);

        dishService.update(dishDto);
        return R.success("修改成功");
    }

    @PutMapping("/editStatus")
    public R<String> updateStatus(HttpServletRequest request,@RequestBody DishDto dishDto){

        log.info("更新状态ids,{}",dishDto.getIds());
        log.info("status,{}",dishDto.getStatus());

        if(dishDto.getIds().size() == 0 || dishDto.getStatus().equals(null) || dishDto.getStatus() < 0){
            throw new CustomerException("缺少ids或status参数错误");
        }else{
            String jwt = request.getHeader("Authorization");
            Claims claims = JwtUtils.parseJwt(jwt);
            Integer id = (Integer) claims.get("id");
            dishDto.setUpdateTime(LocalDateTime.now());
            dishDto.setUpdateUser(id);

            dishService.updateStatus(dishDto);
            return R.success("操作成功");
        }
    }

    @GetMapping("/lists")
    public R<PageBean> lists(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "5") Integer pageSize,
                             @RequestParam(defaultValue = "0") Integer categoryId,
                             String keyWords
    ) {
        PageBean lists = dishService.lists(page, pageSize,categoryId, keyWords);
        return R.success(lists);
    }

    @GetMapping("/lists/{id}")
    public R<DishDto> categoryDetail(@PathVariable Integer id){
        log.info("查看category详情,{}",id);
        DishDto dishDto = dishService.detail(id);
        log.info("dishDto,{}",dishDto);
        return R.success(dishDto);
    }


    @DeleteMapping("/lists/{ids}")
    public R<String> delete(@PathVariable List<Integer> ids){
        log.info("删除菜品,{}",ids);
        dishService.delete(ids);
        return R.success("操作成功");
    }

    /**
     * 根据菜品分类id 查询菜品信息
     * @return
     */
    @GetMapping("/front/dLists/{categoryId}")
    public R<List<DishDto>> frontDishLists(@PathVariable Integer categoryId){
        List<DishDto> lists = dishService.frontLists(categoryId);
        log.info("lists,{}",lists);
        return R.success(lists);
    }
}
